/** Lachlan Hewson-Doherty (h201384171)
 *  Diploma of Digital and Interactive Games
 *  lachlan_doherty@live.com.au
 *  2013
 */

package cabbage.database.operations;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import cabbage.database.utilities.DatabaseConnection;



public class CollectibleCreationOps {
	
	
	
	
	
	private DatabaseConnection db;
	
	
	
	
	public int insertNewFaculty() throws SQLException {
		
			db = new DatabaseConnection
									();

			Connection conn = db.getConnection
											();

			PreparedStatement ps = conn.prepareStatement
											("insert into faculty (faculty_id) values (null)", 
												Statement.RETURN_GENERATED_KEYS);

			ps.execute
					();
			
			ResultSet rs = ps.getGeneratedKeys
											();
			
			//Making sure that the new ID is in the resultset.
			if (rs.next()) {
				int newID = rs.getInt(1);
				conn.close();
				return newID;
			}

			conn.close();
			return 0;
	}
	
	
	
	
	
	
	
	
	
	
	
	public int insertNewSkill(int faculty_id) throws SQLException {
		
		
		db = new DatabaseConnection
								();

		Connection conn = db.getConnection
										();

		PreparedStatement ps = conn.prepareStatement
										("insert into skill (faculty_id, skill_id) values (?)", 
											Statement.RETURN_GENERATED_KEYS);

		ps.setInt
				(1, faculty_id);

		ps.execute
				();

		ResultSet rs = ps.getGeneratedKeys
										();
		
		//Making sure that the new ID is in the resultset.
		if (rs.next()) {
			int newID = rs.getInt(1);
			conn.close();
			return newID;
		}

		conn.close();
		return 0;
	}
	
	
	
	
	
	
	
	
	
	
	
	public int insertNewItem(int faculty_id, int skill_id) throws SQLException {


		db = new DatabaseConnection
								();

		Connection conn = db.getConnection
										();

		PreparedStatement ps = conn.prepareStatement
										("insert into item (faculty_id, skill_id) values (?,?)", 
											Statement.RETURN_GENERATED_KEYS);

		ps.setInt
				(1, faculty_id);
		
		ps.setInt
				(2, skill_id);
		
		ps.execute
				();

		ResultSet rs = ps.getGeneratedKeys
										();
		
		//Making sure that the new ID is in the resultset.
				if (rs.next()) {
					int newID = rs.getInt(1);
					conn.close();
					return newID;
				}

				conn.close();
				return 0;
	}











	public int insertNewCollectible(int item_id, int skill_id, int faculty_id, int collectibleCategory_id, String name, String description, String imageFileName) throws SQLException {
		

		db = new DatabaseConnection
								();

		Connection conn = db.getConnection
										();

		PreparedStatement ps = conn.prepareStatement
						("insert into collectiblemaster (item_id, skill_id, faculty_id, collectibleCategory_id, name, description, imageFileName) values (?,?,?,?,?,?,?)", 
											Statement.RETURN_GENERATED_KEYS);
		
		ps.setInt
				(1, item_id);

		ps.setInt
				(2, skill_id);
		
		ps.setInt
				(3, faculty_id);
		
		ps.setInt
				(4, collectibleCategory_id);
		
		ps.setString
				(5, name);
		
		ps.setString
				(6, description);
		
		ps.setString
				(7, imageFileName);
		
		ps.execute
				();

		ResultSet rs = ps.getGeneratedKeys
										();
		
		//Making sure that the new ID is in the resultset.
				if (rs.next()) {
					int newID = rs.getInt(1);
					conn.close();
					return newID;
				}

				conn.close();
				return 0;
		
	}

}
